package com.example.day17;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

import com.example.day17.view.XListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener {
    private static int refreshCnt = 0;
    com.example.day17.view.XListView myXlistView;
    private ArrayList<String> items = new ArrayList<String>();
    private int start = 0;
    private Handler mHandler= new Handler();;
    private ArrayAdapter<String> mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        geneItems();
        myXlistView=findViewById(R.id.myxlist);
        mAdapter = new ArrayAdapter<String>(this, R.layout.list_item, items);
        myXlistView.setAdapter(mAdapter);
		myXlistView.setPullLoadEnable(true);
		myXlistView.setPullRefreshEnable(true);
        myXlistView.setXListViewListener(this);
    }
    private void geneItems() {
        for (int i = 0; i != 20; ++i) {
            items.add("refresh cnt " + (++start));
        }
    }
    private void onLoad() {
        myXlistView.stopRefresh();
        myXlistView.stopLoadMore();
        myXlistView.setRefreshTime("刚刚");
    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                start = ++refreshCnt;
                items.clear();
                geneItems();
                // mAdapter.notifyDataSetChanged();
                mAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_item, items);
                myXlistView.setAdapter(mAdapter);
                onLoad();
            }
        }, 2000);
    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                geneItems();
                mAdapter.notifyDataSetChanged();
                onLoad();
            }
        }, 2000);
    }
}
